package br.com.guiav6.sisguia.dao.hibernate;

import java.util.List;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import br.com.guiav6.sisguia.dao.BlocoDao;
import br.com.guiav6.sisguia.entity.Bloco;
import br.com.guiav6.sisguia.repository.hibernate.AbstractDaoHibernate;

@Repository("blocoDao")
public class BlocoDaoHibernate extends AbstractDaoHibernate<Long, Bloco> implements BlocoDao {

	@Override
	public List<Bloco> getPosicaoBlocos(String cep) {
		StringBuilder sql = new StringBuilder("from Bloco b");
		sql.append(" where b.pk.nuCep = :nuCep");
		
		TypedQuery<Bloco> q = getEntityManager().createQuery(sql.toString(), Bloco.class);
		q.setParameter("nuCep", cep);
		
		return q.getResultList();
	}

	@Override
	public Bloco getPosicaoBloco(String cep, String bloco) {
		StringBuilder sql = new StringBuilder("from Bloco b");
		sql.append(" where b.pk.nuCep = :nuCep");
		sql.append(" and b.pk.letra = :letra");
		
		TypedQuery<Bloco> q = getEntityManager().createQuery(sql.toString(), Bloco.class);
		q.setParameter("nuCep", cep);
		q.setParameter("letra", bloco);
		
		return q.getSingleResult();
	}

}